<?php
/**
 * Site3TempoParser.php
 *
 * Description
 *
 * @author Milkmeowo <milkmeowo@gmail.com>
 */

namespace App\Parser;
use App\Exceptions\NoPriceException;
use Symfony\Component\DomCrawler\Crawler;


class Site3TempoParser
{

    public static function getAllProducts($html, &$array)
    {
        $parser = new Crawler($html);
        $node = $parser->filter('li[id^=anchorBoxId]')->each(function (Crawler $node) use (&$array) {
            self::parserProductNode($node, $array);
        });
    }

    public static function getFirstProduct($html, &$array)
    {
        $parser = new Crawler($html);
        $node = $parser->filter('li[id^=anchorBoxId]')->first();
        self::parserProductNode($node, $array);
    }

    public static function getLastProducts($html, &$array)
    {
        $parser = new Crawler($html);
        $node = $parser->filter('li[id^=anchorBoxId]')->last();
        self::parserProductNode($node, $array);
    }

    private static function parserProductNode($node , &$array)
    {
        $originId = $node->attr('id');
        $id = (int) str_replace('anchorBoxId_','',$originId);
        $url = $node->filter('.name a')->attr('href');
        $name = $node->filter('.name a')->text();
        $thumb = $node->filter('img.thumb')->attr('src');
        $description = $node->filter('.xans-product-listitem')->text();
        //preg_match('/\d+(,\d+)*원/', $price, $matches);
        if(preg_match('/[\d,]+원/', $description, $matches)){
            if(isset($matches[0]) === false) {
                throw new NoPriceException();
            }
            $price = $matches[0];
            $price = (int) str_replace(array(',','원') , '' , $price);
        }
        $array[] = [
            'id' => $id,
            'originId' => $originId,
            'name' => $name,
            'url' => $url,
            'thumb' => $thumb,
            'description' => $description,
            'price' => $price,
        ];
    }

    public static function hasNextPage($html)
    {
        $parser = new Crawler($html);

        $node = $parser->filter('.xans-product-normalpaging .last')->previousAll();
        if ($node->children()->attr('href') !== '#none') {
            return $node->children()->attr('href');
        }

        return false;
    }

    public static function getLastPage($html)
    {
        $parser = new Crawler($html);

        $node = $parser->filter('.xans-product-normalpaging .last');
        if ($node->children()->attr('href') !== '#none') {
            return $node->children()->attr('href');
        }

        return null;
    }

    public static function getFirstPage($html)
    {
        $parser = new Crawler($html);

        $node = $parser->filter('.xans-product-normalpaging .first');
        if ($node->children()->attr('href') !== '#none') {
            return $node->children()->attr('href');
        }

        return null;
    }

    public static function getNameOfCategory($html)
    {
        $parser = new Crawler($html);
        $node = $parser->filter('h3.xans-product-headcategory');
        return trim($node->text());
    }
}